<?php

namespace App\Http\Controllers\Api;

use App\Models\AppQa;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;

class HomeController extends BaseController
{
    /**
     * @return JsonResponse
     */
    public function index() {


        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => [
                'banner' => app_setting('banner'),
                'home_h5_diy' => app_setting('home_h5_diy'),
                'home_web_diy' => app_setting('home_web_diy'),
                'app_name' => app_setting('app_name'),
                'login_logo' => app_setting('login_logo'),
                'register_logo' => app_setting('register_logo')
            ]
        ]);
    }

    /**
     * @return JsonResponse
     */
    public function privacy() {
        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => [
                'privacy' => app_setting('privacy'),
            ]
        ]);
    }

    /**
     * @return JsonResponse
     */
    public function license() {
        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => [
                'license' => app_setting('license'),
            ]
        ]);
    }

    /**
     * @return JsonResponse
     */
    public function rules() {
        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => [
                'rules' => app_setting('rules'),
            ]
        ]);
    }

    /**
     * @return JsonResponse
     */
    public function about() {
        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => [
                'privacy' => app_setting('privacy'),
                'license' => app_setting('license'),
                'rules' => app_setting('rules'),
            ]
        ]);
    }

    /**
     * 常见问题列表
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function qa(Request $request) {
        $pageSize = (int) $request->input('pageSize', 20);

        /* @var $pages \Illuminate\Contracts\Pagination\LengthAwarePaginator */
        $pages = AppQa::orderBy('order')->orderByDesc('id')->paginate($pageSize, ['id', 'title', 'content']);

        return response()->json([
            'code' => 0,
            'msg' => 'ok',
            'data' => [
                'items' =>  $pages->items(),
            ]
        ]);
    }
}
